package org.wutopia.labcloud.domain.uaa.controller;


import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserCreateRoleInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserRoleBindAuthoritiesInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserRoleListOutputVO;
import org.wutopia.labcloud.domain.uaa.service.UserRoleService;
import org.wutopia.labcloud.library.common.base.CodedMessage;
import org.wutopia.labcloud.library.common.base.CommonResponse;
import org.wutopia.labcloud.library.common.base.PageInputVO;
import org.wutopia.labcloud.library.common.base.PageOutputVO;

@RestController
@RequestMapping("/user/auth/role")
@AllArgsConstructor
public class UserRoleController {

    private UserRoleService userRoleService;

    @PostMapping("/create")
    public ResponseEntity<CodedMessage<String>> createRole(@RequestBody UserCreateRoleInputVO vo) {

        if(vo == null || vo .getRoleName() == null ) {
            return CommonResponse.failure("入参错误");
        }

        userRoleService.createRole(vo);

        return CommonResponse.success();
    }

    @PostMapping("/list")
    @Cacheable(key = "#vo.currentPage + #vo.currentNumber")
    public ResponseEntity<CodedMessage<PageOutputVO<UserRoleListOutputVO>>> list(@RequestBody PageInputVO vo) {

        if(vo == null || vo.getPage() == null || vo.getSize() == null ) {
            return CommonResponse.failure("入参错误");
        }

        PageOutputVO<UserRoleListOutputVO> list = userRoleService.list(vo);

        return CommonResponse.successWithData(list);
    }

    @PostMapping("/bind_authorities")
    public ResponseEntity<CodedMessage<String>> bindAuthorities(@RequestBody  UserRoleBindAuthoritiesInputVO vo) {

        if(vo == null || vo.getRoleId() == null || vo.getAuthorityIds() == null ) {
            return CommonResponse.failure("入参错误");
        }

        userRoleService.bindAuthority(vo);

        return CommonResponse.success();
    }


}
